package com.bravo.base.controller;

import com.bravo.base.entity.Label;
import com.bravo.base.service.LabelService;
import com.bravo.common.entity.PageResult;
import com.bravo.common.entity.Result;
import com.bravo.common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 查询全部列表
     * @return
     */
    @GetMapping
    public Result<List> findAll(){
        return new Result<>(true, StatusCode.OK,"查询成功",labelService.findAll());
    }

    /**
     * 根据ID查询标签
     * @param id
     * @return
     */
    @GetMapping(value ="/{id}")
    public Result<Label> findById(@PathVariable String id){
        return new Result<>(true,StatusCode.OK,"查询成功",labelService.findById(id));
    }

    /**
     * 增加标签
     * @param label
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result<>(true,StatusCode.OK,"增加成功");
    }

    /**
     * 修改标签
     * @param label 标签实体
     * @param id 用户id
     * @return 返回result对象
     */
    @PutMapping(value ="/{id}")
    public Result update(@RequestBody Label label,@PathVariable String id){
        label.setId(id);
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除标签
     * @param id 用户id
     * @return 返回result对象
     */
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List> findSearch(@RequestBody Map searchMap){
        return new Result<>(true,StatusCode.OK,"查询成功",labelService.findSearch(searchMap));
    }

    /**
     * 条件+分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<List> findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page pageList = labelService.findsearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }









}
